package mka.puzzle.shape;

import java.awt.Graphics;
import java.util.ArrayList;

public class Rectangle extends Shape {
	private double width;
	private double height;
	private Point leftTopPoint;
	private Point leftBottomPoint;
	private Point rightTopPoint;
	private Point rightBottomPoint;

	/***************************************************************
	 * Constrtuctor.
	 ***************************************************************/
	public Rectangle() {
		this.leftTopPoint = this.leftBottomPoint = this.rightTopPoint = this.rightBottomPoint = new Point(
				0, 0);
		this.width = this.height = 0;
	}

	public Rectangle(Point leftTop, double w, double h) {
		this.leftTopPoint = new Point(leftTop.getX(), leftTop.getY());
		this.width = w;
		this.height = h;
		this.leftBottomPoint = new Point(this.leftTopPoint.getX(),
				this.leftTopPoint.getY() + this.height);
		this.rightTopPoint = new Point(this.leftTopPoint.getX() + this.width,
				this.leftTopPoint.getY());
		this.rightBottomPoint = new Point(
				this.leftTopPoint.getX() + this.width, this.leftTopPoint.getY()
						+ this.height);
	}

	public Rectangle(Point leftTop, Point leftBottom, Point rightTop,
			Point rightBottom) {
		this.leftTopPoint = new Point(leftTop.getX(), leftTop.getY());
		this.leftBottomPoint = new Point(leftBottom.getX(), leftBottom.getY());
		this.rightTopPoint = new Point(rightTop.getX(), rightTop.getY());
		this.rightBottomPoint = new Point(rightBottom.getX(),
				rightBottom.getY());
		this.width = Math.abs(this.leftTopPoint.getX()
				- this.rightTopPoint.getX());
		this.height = Math.abs(this.leftTopPoint.getY()
				- this.leftBottomPoint.getY());
	}

	/***************************************************************
	 * Set & Get method.
	 ***************************************************************/
	public double getWidth() {
		return width;
	}

	public void setWidth(double width) {
		this.width = width;
	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}

	public Point getLeftTopPoint() {
		return leftTopPoint;
	}

	public void setLeftTopPoint(Point leftTop) {
		this.leftTopPoint = leftTop;
	}

	public Point getLeftBottomPoint() {
		return leftBottomPoint;
	}

	public Point getRightTopPoint() {
		return rightTopPoint;
	}

	public Point getRightBottomPoint() {
		return rightBottomPoint;
	}

	/***************************************************************
	 * Override from superclass.
	 ***************************************************************/
	@Override
	public String getName() {
		return "Rectangle";
	}

	@Override
	public double getPerimeter() {
		return (this.width + this.height) * 2;
	}

	@Override
	public double getArea() {
		return this.width * this.height;
	}

	@Override
	public ArrayList<Point> allPointBound() {
		ArrayList<Point> pointList = new ArrayList<Point>();
		// Min value of X
		double minX = this.leftTopPoint.getX();
		// Max value of X
		double maxX = this.rightTopPoint.getX();
		// Min value of Y
		double minY = this.leftTopPoint.getY();
		// Max value of Y
		double maxY = this.leftBottomPoint.getY();

		for (double x = minX; x <= maxX; x++) {
			for (double y = minY; y <= maxY; y++) {
				if (x == minX)
					pointList.add(new Point(x, y));
				else if (x == maxX)
					pointList.add(new Point(x, y));
				else if (y == minY)
					pointList.add(new Point(x, y));
				else if (y == maxY)
					pointList.add(new Point(x, y));
			}
		}
		return pointList;
	}

	@Override
	public ArrayList<Point> allPointShape() {
		ArrayList<Point> pointList = new ArrayList<Point>();
		// Min value of X
		double minX = this.leftTopPoint.getX();
		// Max value of X
		double maxX = this.leftTopPoint.getX() + this.width;
		// Min value of Y
		double minY = this.leftTopPoint.getY();
		// Max value of Y
		double maxY = this.leftTopPoint.getY() + this.height;

		for (double x = minX; x <= maxX; x++) {
			for (double y = minY; y <= maxY; y++) {
				pointList.add(new Point(x, y));
			}
		}
		return pointList;
	}

	/***************************************************************
	 * Draw and paint method.
	 ***************************************************************/
	/***
	 * draw.
	 * 
	 * Draw the Rectangle by java function
	 * 
	 * @param g Graphics
	 */
	@Override
	public void draw(Graphics g) {
		g.drawRect((int)this.leftTopPoint.getX(), (int)this.leftTopPoint.getY(),
				(int)this.width, (int)this.height);

	}
}
